package com.goods.business.service.imp;

import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author liangliang
 * @Date 2021/11/9 8:19 Administrator
 * @Description
 */
@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;
    /**
     * 分页查询物资来源信息
     * @param
     * @param
     * @param
     * @param
     * @param
     * @return
     */
    @Override
    public PageVO<SupplierVO> findSupplierList(Integer pageNum,Integer pageSize, SupplierVO supplierVOs) {
        List<Supplier> supplierList = supplierMapper.LikeBySupplier(supplierVOs);

        if (!CollectionUtils.isEmpty(supplierList)){

            List<SupplierVO> supplierVOList = supplierList.stream().map(supplier -> {
                SupplierVO supplierVO = new SupplierVO();
                BeanUtils.copyProperties(supplier,supplierVO);
                return supplierVO;
            }).collect(Collectors.toList());

            //总条数
            int total=supplierVOList.size();
            //每页记录数
            int size=(pageNum-1)*pageSize+pageSize;
            List<SupplierVO> supplierVOList1=supplierVOList.subList((pageNum-1)*pageSize,size>total?total:size);
            PageVO<SupplierVO> pageVO = new PageVO<>(total,supplierVOList1);
            return pageVO;
        }
        return null;
    }

    /**
     * 添加物资来源信息
     * @param supplierVO
     */
    @Override
    public void add(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        supplier.setModifiedTime(new Date());
        supplier.setCreateTime(new Date());
        supplierMapper.insert(supplier);
    }

    /**
     * 通过id查询物资来源信息用作修改的数据回显
     * @param id
     * @return
     */
    @Override
    public SupplierVO edit(Integer id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier,supplierVO);
        return supplierVO;
    }

    /**
     * 修改物资来源信息
     * @param id
     * @param supplierVO
     */
    @Override
    public void update(Long id, SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        supplier.setId(id);
       supplier.setModifiedTime(new Date());
        supplierMapper.updateByPrimaryKey(supplier);
    }

    /**
     * 删除物资来源信息
     * @param id
     */
    @Override
    public void delete(Long id) {
       supplierMapper.deleteByPrimaryKey(id);

    }

    /**
     * 查询来源信息用于入库显示
     * @return
     */
    @Override
    public List<SupplierVO> findAll() {
        Example example = new Example(Supplier.class);
        List<Supplier> supplierList = supplierMapper.selectByExample(example);
        List<SupplierVO> supplierVOList = supplierList.stream().map(supplier -> {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVO);
            return supplierVO;
        }).collect(Collectors.toList());
    return supplierVOList;
    }
}
